<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="课程" prop="course">
        <el-select v-model="queryParams.courseId" placeholder="课程">
            <el-option v-for="item in courseList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
      </el-form-item> -->
      <el-form-item label="中文" prop="chinese">
        <el-input
          v-model="queryParams.chinese"
          placeholder="请输入中文"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="英文" prop="english">
        <el-input
          v-model="queryParams.english"
          placeholder="请输入英文"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音标" prop="phoneticSymbol">
        <el-input
          v-model="queryParams.phoneticSymbol"
          placeholder="请输入音标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发音" prop="pronounce">
        <!-- <el-input
          v-model="queryParams.pronounce"
          placeholder="请输入发音"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fileMaintenance:learningMaterials:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fileMaintenance:learningMaterials:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fileMaintenance:learningMaterials:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fileMaintenance:learningMaterials:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="learningMaterialsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="课程" align="center" prop="courseName" />
      <el-table-column label="中文" align="center" prop="chinese" />
      <el-table-column label="英文" align="center" prop="english" />
      <el-table-column label="音标" align="center" prop="phoneticSymbol" />
      <!-- <el-table-column label="发音" align="center" prop="pronounce" /> -->
      <el-table-column label="例句-英" align="center" prop="exampleSentenceHero" />
      <el-table-column label="例句-中" align="center" prop="exampleSentenceMiddle" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fileMaintenance:learningMaterials:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fileMaintenance:learningMaterials:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学习资料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="课程" prop="course">
              <el-select v-model="form.courseId" placeholder="请选择课程" style="width: 100%;">
                <el-option v-for="item in courseList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-form-item label="中文" prop="chinese">
          <el-input v-model="form.chinese" placeholder="请输入中文" />
        </el-form-item> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文" prop="english">
              <el-input v-model="form.english" placeholder="请输入英文" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="音标" prop="phoneticSymbol">
              <el-input v-model="form.phoneticSymbol" placeholder="请输入音标" />
            </el-form-item>
          </el-col>
         <!-- <el-col :span="12">
            <el-form-item label="发音" prop="pronounce">
              <el-input v-model="form.pronounce" placeholder="请输入发音" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item style="margin-bottom: 5px;">
              <el-button type="primary" icon="el-icon-plus" @click="addCourseSelse">添加词性</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <div v-for="(item, index) in partOfSpeechSelectList" :key="item.id">
          <el-row>
            <el-col :span="10">
              <el-form-item label="词性">
                <el-select v-model="item.partOfSpeech" placeholder="请选择词性">
                  <el-option v-for="item2 in partOfSpeechList" :key="item2.id" :label="item2.name" :value="item2.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="中文意思">
                <el-input v-model="item.meaningInChinese" placeholder="请输入中文意思" />
              </el-form-item>
            </el-col>
            <el-col :span="2" v-if="index > 0" style="text-align: right;">
              <el-button type="primary" icon="el-icon-remove-outline" circle @click="delCourseSelse(index)"></el-button>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="例句-英" prop="exampleSentenceHero">
              <el-input v-model="form.exampleSentenceHero" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="例句-中" prop="exampleSentenceMiddle">
              <el-input v-model="form.exampleSentenceMiddle" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLearningMaterials, getLearningMaterials, delLearningMaterials, addLearningMaterials, updateLearningMaterials } from "@/api/fileMaintenance/learningMaterials";
import { listCourseAll } from "@/api/fileMaintenance/course";

export default {
  name: "LearningMaterials",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习资料表格数据
      learningMaterialsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseId: null,
        chinese: null,
        english: null,
        phoneticSymbol: null,
        pronounce: null,
        exampleSentenceHero: null,
        exampleSentenceMiddle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 课程列表数据
      courseList: [],
      partOfSpeechSelectList: [
        { partOfSpeech: '', meaningInChinese: '' }
      ],
      partOfSpeechList: [
        { id: 'a.', name: 'a.' },
        { id: 'n.', name: 'n.' },
        { id: 'v.', name: 'v.' },
        { id: 'ad.', name: 'ad.' },
        { id: 'vt.', name: 'vt.' },
        { id: 'vi.', name: 'vi.' },
        { id: 'pron.', name: 'pron.' },
        { id: 'num.', name: 'num.' },
        { id: 'art.', name: 'art.' },
        { id: 'prep.', name: 'prep.' },
        { id: 'conj.', name: 'conj.' },
        { id: 'int.', name: 'int.' }
      ]
    };
  },
  created() {
    this.getList();
    this.getCourseList();
  },
  methods: {
    /** 查询学习资料列表 */
    getList() {
      this.loading = true;
      listLearningMaterials(this.queryParams).then(response => {
        this.learningMaterialsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询课程列表 */
    getCourseList() {
      listCourseAll().then(response => {
        this.courseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        chinese: null,
        english: null,
        phoneticSymbol: null,
        pronounce: null,
        exampleSentenceHero: null,
        exampleSentenceMiddle: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.partOfSpeechSelectList = [
        { partOfSpeech: '', meaningInChinese: '' }
      ];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学习资料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLearningMaterials(id).then(response => {
        this.partOfSpeechSelectList = response.data.partOfSpeechSelectList;
        this.form = response.data;
        this.open = true;
        this.title = "修改学习资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let formParameter = this.form;
          formParameter.partOfSpeechSelectList = this.partOfSpeechSelectList;
          if (this.form.id != null) {
            updateLearningMaterials(formParameter).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLearningMaterials(formParameter).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学习资料编号为"' + ids + '"的数据项？').then(function() {
        return delLearningMaterials(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fileMaintenance/learningMaterials/export', {
        ...this.queryParams
      }, `learningMaterials_${new Date().getTime()}.xlsx`)
    },
    /** 动态添加 */
    addCourseSelse() {
      this.partOfSpeechSelectList.push({ partOfSpeech: '', meaningInChinese: '' });
    },
    /** 动态删除*/
    delCourseSelse(index) {
      this.partOfSpeechSelectList.splice(index, 1);
    }
  }
};
</script>
